import type { DirectiveBinding } from 'vue'

/* 节流 // 节流指令 一段时间内只执行一次 */
const throttling = {
  mounted(el: HTMLElement, binding: DirectiveBinding) {
    let lastExecutedTime: number = 0
    const HookTime: number = Number(binding.arg) || 1000
    el.addEventListener('click', () => {
      const currentTime = Date.now() // 获取当前时间
      if (currentTime - lastExecutedTime > HookTime) {
        lastExecutedTime = currentTime
        binding.value(binding.arg)
      }
    })
  },
  beforeUnmount(el: HTMLElement) {
    el.removeEventListener('click', () => { })
  }
}

export default throttling
